home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / StringThing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  6.1 KB  |  188 lines  |  [TEXT/KAHL]

  1. /* StringThing.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "StringThing.h"
  31. #include "Memory.h"
  32.  
  33.  
  34. #define INITIALSTRINGTHINGLENGTH (32)
  35.  
  36.  
  37. struct StringThingRec
  38.     {
  39.         char*                    TheBuffer;
  40.         long                    ActualLength;
  41.     };
  42.  
  43.  
  44. /* create a new string thing. */
  45. StringThingRec*            NewStringThing(void)
  46.     {
  47.         StringThingRec*        StringThing;
  48.  
  49.         StringThing = (StringThingRec*)AllocPtrCanFail(sizeof(StringThingRec),
  50.             "StringThingRec");
  51.         if (StringThing == NIL)
  52.             {
  53.              FailurePoint1:
  54.                 return NIL;
  55.             }
  56.         StringThing->TheBuffer = AllocPtrCanFail(INITIALSTRINGTHINGLENGTH,
  57.             "StringThingRec buffer");
  58.         if (StringThing->TheBuffer == NIL)
  59.             {
  60.              FailurePoint2:
  61.                 ReleasePtr(StringThing->TheBuffer);
  62.                 goto FailurePoint1;
  63.             }
  64.         StringThing->ActualLength = 0;
  65.         return StringThing;
  66.     }
  67.  
  68.  
  69. /* dispose of a string thing */
  70. void                                DisposeStringThing(StringThingRec* StringThing)
  71.     {
  72.         CheckPtrExistence(StringThing);
  73.         ReleasePtr(StringThing->TheBuffer);
  74.         ReleasePtr((char*)StringThing);
  75.     }
  76.  
  77.  
  78. /* add some data onto the end of the string thing */
  79. MyBoolean                        AppendStringThing(StringThingRec* StringThing,
  80.                                             char* Data, long Length)
  81.     {
  82.         char*                            NewBuffer;
  83.         long                            CurrentSize;
  84.  
  85.         CheckPtrExistence(StringThing);
  86.         CurrentSize = PtrSize(StringThing->TheBuffer);
  87.         if (StringThing->ActualLength + Length > CurrentSize)
  88.             {
  89.                 long                            NewSize;
  90.  
  91.                 if (StringThing->ActualLength + Length > CurrentSize * 2)
  92.                     {
  93.                         NewSize = StringThing->ActualLength + Length;
  94.                     }
  95.                  else
  96.                     {
  97.                         NewSize = CurrentSize * 2;
  98.                     }
  99.                 NewBuffer = AllocPtrCanFail(NewSize,"StringThingRec buffer");
  100.                 if (NewBuffer == NIL)
  101.                     {
  102.                         return False;
  103.                     }
  104.                 CopyData(&(StringThing->TheBuffer[0]),&(NewBuffer[0]),CurrentSize);
  105.                 ReleasePtr(StringThing->TheBuffer);
  106.                 StringThing->TheBuffer = NewBuffer;
  107.             }
  108.         PRNGCHK(StringThing->TheBuffer,&(StringThing->TheBuffer[StringThing->ActualLength]),
  109.             Length);
  110.         CopyData(&(Data[0]),&(StringThing->TheBuffer[StringThing->ActualLength]),Length);
  111.         StringThing->ActualLength += Length;
  112.         return True;
  113.     }
  114.  
  115.  
  116. /* look up a byte in the string thing */
  117. char                                LookupInStringThing(StringThingRec* StringThing, long Index)
  118.     {
  119.         CheckPtrExistence(StringThing);
  120.         ERROR((Index < 0) || (Index >= StringThing->ActualLength),PRERR(ForceAbort,
  121.             "LookupInStringThing:  array bounds violation"));
  122.         PRNGCHK(StringThing->TheBuffer,&(StringThing->TheBuffer[Index]),sizeof(char));
  123.         return StringThing->TheBuffer[Index];
  124.     }
  125.  
  126.  
  127. /* find out how long the string thing is */
  128. long                                GetStringThingLength(StringThingRec* StringThing)
  129.     {
  130.         CheckPtrExistence(StringThing);
  131.         return StringThing->ActualLength;
  132.     }
  133.  
  134.  
  135. /* delete a block of data from the string thing */
  136. void                                StringThingDeleteStuff(StringThingRec* StringThing,
  137.                                             long StartIndex, long NumBytes)
  138.     {
  139.         CheckPtrExistence(StringThing);
  140.         ERROR((StartIndex < 0) || (StartIndex + NumBytes >= StringThing->ActualLength)
  141.             || (NumBytes > StringThing->ActualLength) || (NumBytes < 0),PRERR(ForceAbort,
  142.             "StringThingDeleteStuff:  array bounds violation"));
  143.         MoveData(&(StringThing->TheBuffer[StartIndex + NumBytes]),
  144.             &(StringThing->TheBuffer[StartIndex]),NumBytes);
  145.         StringThing->ActualLength -= NumBytes;
  146.     }
  147.  
  148.  
  149. /* return the index of a character in the buffer or -1 if it's not found */
  150. long                                StringThingSearchForChar(StringThingRec* StringThing, char Thing)
  151.     {
  152.         long                            Limit;
  153.         long                            Scan;
  154.  
  155.         CheckPtrExistence(StringThing);
  156.         Limit = StringThing->ActualLength;
  157.         for (Scan = 0; Scan < Limit; Scan += 1)
  158.             {
  159.                 if (Thing == StringThing->TheBuffer[Scan])
  160.                     {
  161.                         return Scan;
  162.                     }
  163.             }
  164.         return -1;
  165.     }
  166.  
  167.  
  168. /* get a subrange of the string thing */
  169. char*                                StringThingGetSubrange(StringThingRec* StringThing,
  170.                                             long StartIndex, long NumBytes)
  171.     {
  172.         char*                            Buffer;
  173.  
  174.         CheckPtrExistence(StringThing);
  175.         ERROR((StartIndex < 0) || (StartIndex + NumBytes > StringThing->ActualLength)
  176.             || (NumBytes > StringThing->ActualLength) || (NumBytes < 0),PRERR(ForceAbort,
  177.             "StringThingGetSubrange:  array bounds violation"));
  178.         Buffer = AllocPtrCanFail(NumBytes,"StringThingGetSubrange");
  179.         if (Buffer == NIL)
  180.             {
  181.                 return NIL;
  182.             }
  183.         PRNGCHK(StringThing->TheBuffer,&(StringThing->TheBuffer[StartIndex]),NumBytes);
  184.         PRNGCHK(Buffer,&(Buffer[0]),NumBytes);
  185.         MoveData(&(StringThing->TheBuffer[StartIndex]),&(Buffer[0]),NumBytes);
  186.         return Buffer;
  187.     }
  188.